package com.mingtu.guards.activity;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.SPStaticUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.CalendarPickerUtils;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.MonitoringRecFragment1;
import com.mingtu.guards.fragment.MonitoringRecFragment2;
import com.mingtu.guards.fragment.MonitoringRecFragment3;
import com.mingtu.guards.utils.MySPTools;
import com.mingtu.guards.view.FilterCancleView;
import com.mingtu.guards.view.FilterSureView;
import com.mingtu.guards.view.TimeFilterView;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class MonitoringRecRecordActivity extends MyBaseActivity {


    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_fold_dequ)
    TextView tvFoldDequ;
    @BindView(R.id.layout_arrow_dequ)
    LinearLayout layoutArrowDequ;
    @BindView(R.id.recycler_dequ)
    RecyclerView recyclerDequ;
    @BindView(R.id.layout_dequ)
    LinearLayout layoutDequ;
    @BindView(R.id.layout_right_drawer)
    RelativeLayout layoutRightDrawer;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.iv_filte)
    ImageView ivFilte;
    @BindView(R.id.time_filter_time)
    TimeFilterView timeFilterTime;
    @BindView(R.id.filter_cancle_view)
    FilterCancleView filterCancleView;
    @BindView(R.id.filter_sure_view)
    FilterSureView filterSureView;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"重点人员", "陌生人", "车辆"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String beginTime;
    private String endTime;
    private MonitoringRecFragment1 monitoringRecFragment1;
    private MonitoringRecFragment2 monitoringRecFragment2;
    private MonitoringRecFragment3 monitoringRecFragment3;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_monitoring_rec_record;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("识别记录");
    }

    @Override
    protected void initData() {

        SPStaticUtils.put(MySPTools.monitoringRecFliteBeginTime, "");
        SPStaticUtils.put(MySPTools.monitoringRecFliteEndTime, "");

        monitoringRecFragment1 = MonitoringRecFragment1.getInstance(0);
        monitoringRecFragment2 = MonitoringRecFragment2.getInstance(1);
        monitoringRecFragment3 = MonitoringRecFragment3.getInstance(2);

        mFragments.add(monitoringRecFragment1);
        mFragments.add(monitoringRecFragment2);
        mFragments.add(monitoringRecFragment3);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        viewPager.setOffscreenPageLimit(mTitles.length);//需要设置此值
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(onPageChangeListener);

        commonTab.setTabData(mTabEntities);
        commonTab.setOnTabSelectListener(new MyOnTabSelectListener());
        setTextBold(0);

        timeFilterTime.setOnTimeResultListener(new TimeFilterView.OnTimeResultListener() {
            @Override
            public void onResult(Date startDate, Date endDate) {
                beginTime = UIUtils.getTime2(startDate);
                endTime = UIUtils.getTime2(endDate);

                SPStaticUtils.put(MySPTools.monitoringRecFliteBeginTime, beginTime);
                SPStaticUtils.put(MySPTools.monitoringRecFliteEndTime, endTime);
            }
        });

        filterCancleView.setOnClickListener(new FilterCancleView.OnClickListener() {
            @Override
            public void onClickCancel() {
                closeDrawers();
            }
        });

        filterSureView.setOnClickListener(new FilterSureView.OnClickListener() {
            @Override
            public void onClickReset() {
                closeDrawers();
                resetData();
                CalendarPickerUtils.getInstance().resetData();
            }

            @Override
            public void onClickSure() {
                boolean isResumed1 = monitoringRecFragment1.isResumed(); //判断fragment是否已经加载
                boolean isResumed2 = monitoringRecFragment2.isResumed();
                boolean isResumed3 = monitoringRecFragment3.isResumed();

                if (isResumed1) {
                    monitoringRecFragment1.refreshData();
                }
                if (isResumed2) {
                    monitoringRecFragment2.refreshData();
                }
                if (isResumed3) {
                    monitoringRecFragment3.refreshData();
                }

                closeDrawers();
            }
        });

    }

    @Override
    protected void getData() {

    }


    @OnClick({R.id.layout_right_drawer, R.id.iv_filte})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_right_drawer:
                break;
            case R.id.iv_filte:
                boolean isOpen = drawerLayout.isDrawerOpen(layoutRightDrawer);
                if (isOpen) {
                    closeDrawers();
                } else {
                    openDrawer();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SPStaticUtils.remove(MySPTools.monitoringRecFliteBeginTime);
        SPStaticUtils.remove(MySPTools.monitoringRecFliteEndTime);
    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position);
        }

        @Override
        public void onTabReselect(int position) {

        }
    }


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
            setTextBold(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**
     * 关闭侧边栏
     */
    public void closeDrawers() {
        drawerLayout.closeDrawer(layoutRightDrawer, true);
    }

    /**
     * 打开侧边栏
     */
    public void openDrawer() {
        drawerLayout.openDrawer(layoutRightDrawer);
    }


    /**
     * 重置数据
     */
    private void resetData() {
        SPStaticUtils.put(MySPTools.monitoringRecFliteBeginTime, "");
        SPStaticUtils.put(MySPTools.monitoringRecFliteEndTime, "");
        timeFilterTime.resetData();
        monitoringRecFragment1.refreshData();
        monitoringRecFragment2.refreshData();
        monitoringRecFragment3.refreshData();
    }

    /**
     * tab选中加粗
     *
     * @param position
     */
    private void setTextBold(int position) {
        for (int i = 0; i < commonTab.getTabCount(); i++) {
            TextView textView = commonTab.getTitleView(i);
            if (position == i) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_15));
                textView.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.sp_14));
                textView.setTypeface(Typeface.DEFAULT);
            }
        }
    }

}